<script lang="ts" setup>
import useStore from '@/store/index';
import { onMounted, computed, ref } from 'vue';
import type { Category, CategoryList } from '@/types/index';
const home = useStore().home;
home.getAllCategory();
let checkId = ref<string>('-1');
let leftCategory = computed(() => {
  // 基于 Pinia 中的分类数据处理
  return home.categoryList.map((item) => ({
    id: item.id,
    name: item.name,
    goods: item.goods,
    picture: item.picture,
    // 只需要截取前两个二级分类
    children: item.children.slice(0, 2),
  }));
});

let isShowCate = ref(false);
let hoverCate = ref({});
const hoverChangeGood = (id: string) => {
  console.log(11);
  checkId.value = id;
  console.log(checkId.value, 'dwad');
  isShowCate.value = true;
  let arr = leftCategory.value.filter((item) => item.id == id);
  hoverCate.value = arr[0];
  console.log(hoverCate.value);
};

const moveLeaveGood = () => {
  checkId.value = '-1';
  isShowCate.value = false;
  hoverCate.value = [];
  // timer = null;
};
console.log(leftCategory, '@');
</script>

<template>
  <div class="category" @mouseleave="moveLeaveGood">
    <ul class="menu" v-if="leftCategory.length > 0">
      <li
        v-for="item in leftCategory"
        :key="item.id"
        @mouseenter="hoverChangeGood(item.id)"
        :class="{ checkedhover: checkId == item.id }"
      >
        <RouterLink to="">{{ item.name }}</RouterLink>
        <RouterLink
          to=""
          v-for="secondItem in item.children"
          :key="secondItem.id"
          >{{ secondItem.name }}</RouterLink
        >
      </li>
      <li><a>品牌</a></li>
    </ul>
    <!-- 分类占位效果 v-else 互斥 -->
    <ul class="menu" v-else>
      <li v-for="i in 9" :key="i">
        <XtxSkeleton
          :width="40"
          :height="20"
          style="margin-right: 5px"
          bg="rgba(255,255,255,0.2)"
        />
        <XtxSkeleton
          :width="50"
          :height="20"
          bg="rgba(255,255,255,0.2)"
          style="margin-right: 5px"
        />
        <XtxSkeleton :width="50" :height="20" bg="rgba(255,255,255,0.2)" />
      </li>
    </ul>
    <div class="layer" v-if="isShowCate">
      <div class="layer-top">
        <h3>分类推荐</h3>
        <p>根据您的购买或浏览记录推荐</p>
      </div>
      <div class="layer-goods">
        <div class="layer-good" v-for="good in hoverCate.goods" :key="good.id">
          <div class="pic"><img :src="good.picture" alt="" /></div>
          <div class="info">
            <h3 class="info-title">{{ good.name }}</h3>
            <p class="info-attribute ellipsis">{{ good.desc }}</p>
            <div class="price">
              ￥<i>{{ good.price }}</i>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.category {
  width: 100%;
  display: flex;
}
.menu {
  width: 250px;
  background-color: rgba(0, 0, 0, 0.8);
  li {
    &.checkedhover {
      background-color: #27ba9b;
    }
    height: 50px;
    padding-left: 40px;
    line-height: 50px;
    background-color: transparent;
    // &:hover {
    //   background: @xtxColor;
    // }
    a {
      color: #fff;
      margin-right: 5px;
      font-size: 14px;
      &:first-child {
        font-size: 16px;
      }
    }
  }
}

.layer {
  height: 500px;
  padding: 20px;
  flex: 1;
  background-color: rgba(255, 255, 255, 0.8);
  .layer-top {
    display: flex;
    line-height: 50px;
    h3 {
      margin-right: 20px;
      font-size: 20px;
    }
    p {
      font-size: 16px;
      color: #666;
    }
  }

  // 商品
  .layer-goods {
    display: flex;
    flex-wrap: wrap;
    .layer-good {
      &:hover {
        background: #e3f9f4;
      }
      padding: 10px;
      display: flex;
      width: 310px;
      height: 120px;
      margin-right: 15px;
      margin-bottom: 15px;
      border: 1px solid #eee;
      border-radius: 4px;
      background: #fff;
      &:nth-child(3n) {
        margin-right: 0;
      }
      .pic {
        width: 100px;
        height: 100px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .info {
        flex: 1;
        padding-left: 10px;
        line-height: 24px;
        overflow: hidden;
        .ellipsis {
          width: 100%;
          max-width: 100%;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .info-title {
          font-size: 16px;
        }
        .info-attribute {
          color: #999;
        }
        .price {
          color: @priceColor;
          i {
            font-size: 22px;
          }
        }
      }
    }
  }
}
</style>
